package com.hong.common.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.hong.common.util.ShiroUtil;
import com.hong.entity.SysUser;

public class BaseController {

	protected Logger logger = LoggerFactory.getLogger(this.getClass());

	/**
	 * 获取分页页面
	 * 
	 * @param limit
	 * @param offset
	 * @return
	 */
	protected Integer getPageNo(Integer limit, Integer offset) {
		return offset == 0 ? 1 : offset / limit + 1;
	}

	/**
	 * 重定向至地址 url
	 * 
	 * @param url 请求地址
	 * @return
	 */
	protected String redirectTo(String url) {
		StringBuffer rto = new StringBuffer("redirect:");
		rto.append(url);
		return rto.toString();
	}

	/**
	 * 
	 * 返回 JSON 格式对象
	 * 
	 * @param object 转换对象
	 * @return
	 */
	protected String toJson(Object object) {
		return JSON.toJSONString(object, SerializerFeature.BrowserCompatible);
	}

	/**
	 * 
	 * 返回 JSON 格式对象
	 * 
	 * @param object   转换对象
	 * @param features 序列化特点
	 * @return
	 */
	protected String toJson(Object object, String format) {
		if (format == null) {
			return toJson(object);
		}
		return JSON.toJSONStringWithDateFormat(object, format, SerializerFeature.WriteDateUseDateFormat);
	}

	/**
	 * 获取用户缓存信息
	 */
	public SysUser getSysUser() {
		return ShiroUtil.getSessionUser();
	}

	/**
	 * 获取登录用户id
	 */
	public String getUserId() {
		return getSysUser().getId();
	}

}
